package com.groupten.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupten.Dao.StudentDao;
import com.groupten.Entity.StudentEntity;
import com.groupten.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.groupten.Utility.PasswordUtils;

@Service("studentService")
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public Integer deleteById(Long id) {
        return studentDao.deleteById(id);
    }

    @Override
    public Integer deleteByName(String name) {
        LambdaQueryWrapper<StudentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(name),StudentEntity::getStudentName,name);
        return studentDao.delete(queryWrapper);
    }

    @Override
    public Integer deleteByNumber(String number) {
        QueryWrapper<StudentEntity> studentNumber = new QueryWrapper<StudentEntity>().eq(number != null && !number.isEmpty(), "student_number", number);
        return studentDao.delete(studentNumber);
    }

    @Override
    public Boolean updateById(Long id, Map<String, String> newData) {
        StudentEntity studentEntity = selectById(id);
        for (Map.Entry<String, String> entry : newData.entrySet()) {
            switch (entry.getKey()){
                case "studentName":
                    studentEntity.setStudentName(entry.getValue());
                    break;
                case "studentNumber":
                    studentEntity.setStudentNumber(entry.getValue());
                    break;
                case "studentGender":
                    studentEntity.setStudentGender(entry.getValue());
                    break;
                case "studentEmail":
                    studentEntity.setStudentEmail(entry.getValue());
                    break;
                case "studentGrade":
                    studentEntity.setStudentGrade(entry.getValue());
                    break;
                case "studentClass":
                    studentEntity.setStudentClass(entry.getValue());
                    break;
                case "studentPassword":
                    studentEntity.setStudentPassword(PasswordUtils.encrypt(entry.getValue()));
                    break;
                default:
                    break;
            }
        }
        return studentDao.updateById(studentEntity)>0;
    }

    @Override
    public Boolean insert(StudentEntity studentEntity) {
        // 加密学生密码
        String encryptedPassword = PasswordUtils.encrypt(studentEntity.getStudentPassword());
        // 设置加密后的密码
        studentEntity.setStudentPassword(encryptedPassword);

        // 插入学生信息到数据库
        int insert = studentDao.insert(studentEntity);
        return insert > 0;
    }


    @Override
    public IPage<StudentEntity> selectStudentsByClass(String StudentClass, Integer page, Integer limit) {
        LambdaQueryWrapper<StudentEntity> studentClass = new LambdaQueryWrapper<StudentEntity>().eq(StudentClass!=null && !StudentClass.isEmpty(),StudentEntity::getStudentClass, StudentClass);

        int curpage=1;
        int curlimit=10;
        if (page!=null&&page>0){    
            curpage=page;
        }
        if (limit!=null&&limit>0){
            curlimit=limit;
        }
        return studentDao.selectPage(new Page<>(curpage,curlimit),studentClass);
    }

    @Override
    public IPage<StudentEntity> selectAll(Integer page, Integer limit) {
        int curpage=1;
        int curlimit=10;
        if (page!=null&&page>0){
            curpage=page;
        }
        if (limit!=null&&limit>0){
            curlimit=limit;
        }
        return studentDao.selectPage(new Page<>(curpage,curlimit), Wrappers.lambdaQuery());
    }

    @Override
    public StudentEntity selectByNumber(String number) {
        QueryWrapper<StudentEntity> studentNumber = new QueryWrapper<StudentEntity>().eq(number!= null &&!number.isEmpty(), "student_number", number);
        return studentDao.selectOne(studentNumber);
    }

    @Override
    public StudentEntity selectByName(String name) {
        QueryWrapper<StudentEntity> studentName = new QueryWrapper<StudentEntity>().eq(name!= null &&!name.isEmpty(), "student_name", name);
        return studentDao.selectOne(studentName);
    }

    @Override
    public StudentEntity selectById(Long id) {

        return studentDao.selectById(id);
    }

    @Override
    public List<StudentEntity> selectAll() {
        return studentDao.selectList(Wrappers.lambdaQuery());
    }


}
